package com.zy.ivideo.service;

import com.zy.ivideo.entity.ResponseEntity;
import com.zy.ivideo.entity.SimpleVideoEntity;
import com.zy.ivideo.entity.unionentity.IUnionVideoSimpleEntity;
import com.zy.ivideo.repository.VideoSimpleRepository;
import com.zy.ivideo.utils.ResponseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.persistence.EntityManager;
import javax.persistence.Query;
import java.util.List;

@Service
public class VideoSimpleService {

    @Autowired
    EntityManager entityManager;

    @Autowired
    VideoSimpleRepository repository;

//    /**
//     * 根据ChannelId获取对应Simple视频信息
//     * @param channelId
//     * @param page
//     * @param pagesize
//     * @return
//     */
//    public ResponseEntity<List<SimpleVideoEntity>> getSimpleVideoByChannelId(String channelId,int page,int pagesize){
//        String sql="select simple.*,author.name,author.description,author.avatar_url from b_simple_video simple,b_video_author author where simple.userid=author.user_id and simple.channelid='"+channelId+"' limit "+(page*pagesize)+","+pagesize;
//        Query query = entityManager.createNativeQuery(sql, SimpleVideoEntity.class);
//        List<SimpleVideoEntity> resultList = query.getResultList();
//        if (null==resultList||resultList.size()==0){
//            return ResponseUtils.failed("没有检索到视频信息");
//        }
//        return ResponseUtils.success(resultList);
//    }
//
//    /**
//     * 获取推荐Simple视频信息
//     * @param page
//     * @param pagesize
//     * @return
//     */
//    public ResponseEntity<List<SimpleVideoEntity>> getRecommendSimpleVideo(int page,int pagesize){
//        String sql="select simple.*,author.name,author.description,author.avatar_url from b_simple_video simple,b_video_author author where simple.userid=author.user_id limit "+(page*pagesize)+","+pagesize;
//        Query query = entityManager.createNativeQuery(sql, SimpleVideoEntity.class);
//        List<SimpleVideoEntity> resultList = query.getResultList();
//        if (null==resultList||resultList.size()==0){
//            return ResponseUtils.failed("没有检索到视频信息");
//        }
//        return ResponseUtils.success(resultList);
//    }

    /**
     * 根据ChannelId获取对应Simple视频信息
     * @param channelId
     * @param page
     * @param pagesize
     * @return
     */
    public ResponseEntity<List<IUnionVideoSimpleEntity>> getSimpleVideoByChannelId(String channelId,int page,int pagesize){
        List<IUnionVideoSimpleEntity> result = repository.getSimpleVideoByChannelId(channelId, page, pagesize);
        if (result==null||result.size()==0){
            return ResponseUtils.failed("没有获取到当前Channel下视频信息");
        }
        return ResponseUtils.success(result);
    }

    /**
     * 获取推荐Simple视频信息
     * @param page
     * @param pagesize
     * @return
     */
    public ResponseEntity<List<IUnionVideoSimpleEntity>> getRecommendSimpleVideo(int page,int pagesize){
        List<IUnionVideoSimpleEntity> result = repository.getRecommendSimpleVideo(page, pagesize);
        if (result==null||result.size()==0){
            return ResponseUtils.failed("没有获取到推荐视频信息");
        }
        return ResponseUtils.success(result);
    }

}
